package com.grape.api;

import com.grape.annotation.OperateMethodName;
import com.grape.constants.Constants;
import com.grape.result.R;
import com.grape.utils.SpringUtil;
import com.grape.utils.UploadUtils;
import com.grape.property.FileProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;

/**
 * 类描述： FileController类
 *
 * @author  putao
 * @email   2628279194@qq.com
 */
@RestController
@RequestMapping("files")
@Api(tags = "文件上传接口")
@RequiredArgsConstructor
public class FileController {

    private final FileProperty fileProperty;

    @PostMapping("image")
    @ApiOperation(value = "上传文件")
    @OperateMethodName(value = "上传文件", url = "system:files:image")
    public R<?> upload(MultipartFile file) throws Exception {
        UploadUtils uploadUtils = SpringUtil.getBean(fileProperty.getIsLocal() ? Constants.UPLOAD_LOCAL_UTILS_NAME_FILE_UTILS : Constants.UPLOAD_LOCAL_UTILS_NAME_MINIO_UTILS, UploadUtils.class);
        return R.resultSuccess(Collections.singletonMap("imageUrl", uploadUtils.upload(file)), null);
    }

}
